//
//  ImageAnimatedShowView.m
//  etionUI
//
//  Created by WangJian on 14-2-14.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "ImageAnimatedShowView.h"

@implementation CImageAnimatedShowView
{
    UIView *_bgView;
    UIView *_maskView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _bgView = [[[UIView alloc] initWithFrame:self.bounds] autorelease];
        _bgView.backgroundColor = [UIColor blackColor];
        _bgView.alpha = 0.0;
        [self addSubview:_bgView];
        
        _maskView = [[[UIView alloc] initWithFrame:self.bounds] autorelease];
        _maskView.clipsToBounds = YES;
        [self addSubview:_maskView];
        
        _contentView = [[[UIView alloc] initWithFrame:_maskView.bounds] autorelease];
        [_maskView addSubview:_contentView];
    }
    return self;
}

- (void)scaleViewToOrgFrame:(CGRect)orgFrame withImage:(UIImage *)image fillStyle:(EImageFillStyle)fillStyle
{
    CGSize contentSize = CGSizeMake(self.width, self.height);
    CGSize imageOrgSize;
    
    CGFloat scale = 1.0;
    CGSize imageDesSize;
    if (fillStyle == EImageFillStyleStretchByCenterScale)
    {
        CGFloat rateW = image.size.width / orgFrame.size.width;
        CGFloat rateH = image.size.height / orgFrame.size.height;
        CGFloat rateScale = rateW < rateH ? 1.0 / rateW : 1.0 / rateH;
        imageOrgSize.width = rateScale * image.size.width;
        imageOrgSize.height = rateScale * image.size.height;
        
        [UIImage caleFitImageSize:image.size targetSize:contentSize width:&imageDesSize.width height:&imageDesSize.height];
        scale = imageOrgSize.width / imageDesSize.width;
    }
    else if (fillStyle == EImageFillStyleScale)
    {
        [UIImage caleFitImageSize:image.size targetSize:orgFrame.size width:&imageOrgSize.width height:&imageOrgSize.height];
        [UIImage caleFitImageSize:image.size targetSize:contentSize width:&imageDesSize.width height:&imageDesSize.height];
        scale = imageOrgSize.width / imageDesSize.width;
    }
    
    CGSize maskViewSize = CGSizeMake(orgFrame.size.width/scale, orgFrame.size.height/scale);
    _maskView.frame = CGRectMake(orgFrame.origin.x-(maskViewSize.width-orgFrame.size.width)/2, orgFrame.origin.y-(maskViewSize.height-orgFrame.size.height)/2, maskViewSize.width, maskViewSize.height);
    self.contentView.frame = CGRectMake((maskViewSize.width-contentSize.width)/2, (maskViewSize.height-contentSize.height)/2, contentSize.width, contentSize.height);
    
    _maskView.transform = CGAffineTransformMakeScale(scale, scale);
}

- (void)showContentViewForImage:(UIImage *)image anchorView:(UIView *)anchorView fillStyle:(EImageFillStyle)fillStyle complete:(void (^)())complete
{
    UIWindow *shareWindow = [UIApplication sharedApplication].keyWindow;
    CGRect displayRect = [shareWindow convertRect:anchorView.bounds fromView:anchorView];
//    [shareWindow addSubview:self.view];
//    self.curImageIndex = index;
//    self.imageBrowseView.contentOffset = CGPointMake((self.view.width)*index, 0);
    if (displayRect.size.width <= 0 || displayRect.size.height <= 0)
    {
        [UIView animateWithDuration:.3 delay:.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            _bgView.alpha = 1.0;
        } completion:^(BOOL finished) {
            complete();
        }];
    }
    else
    {
        [self scaleViewToOrgFrame:displayRect withImage:image fillStyle:(EImageFillStyle)fillStyle];
        [UIView animateWithDuration:.3 delay:.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            _bgView.alpha = 1.0;
            _maskView.transform = CGAffineTransformMakeScale(1.0, 1.0);
            _maskView.frame = self.bounds;
            self.contentView.frame = _maskView.bounds;
        } completion:^(BOOL finished) {
            complete();
        }];
    }
}

- (void)dismissContentViewForImage:(UIImage *)image anchorView:(UIView *)anchorView fillStyle:(EImageFillStyle)fillStyle complete:(void (^)())complete
{
    UIWindow *shareWindow = [UIApplication sharedApplication].keyWindow;
    CGRect displayRect = anchorView == nil ? CGRectZero : [shareWindow convertRect:anchorView.bounds fromView:anchorView];

    [UIView animateWithDuration:.3 delay:.0 options:UIViewAnimationOptionCurveEaseInOut animations:^
    {
        if (displayRect.size.width > 0 && displayRect.size.height > 0)
        {
            [self scaleViewToOrgFrame:displayRect withImage:image fillStyle:(EImageFillStyle)fillStyle];
        }
        _bgView.alpha = 0.0;
    } completion:^(BOOL finished)
    {
        complete();
    }];
}

@end
